<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('equipment.number')" prop="id">
        <el-input
          v-model="queryParams.id"
          :placeholder="$t('equipment.numberInput')"
          clearable
          style="width: 170px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('program.reviewer')" prop="checkBy">
        <el-input
          v-model="queryParams.checkBy"
          :placeholder="$t('program.reviewerInput')"
          clearable
          style="width: 170px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('program.status')" prop="status">
        <el-select
          v-model="queryParams.status"
          :placeholder="$t('edition.loginStatus')"
          clearable
          size="small"
          style="width: 170px;"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('equipment.isRead')" prop="isRead">
        <el-select
          v-model="queryParams.isRead"
          :placeholder="$t('edition.isReadStatus')"
          clearable
          size="small"
          style="width: 170px;"
        >
          <el-option
            v-for="dict in isReadOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('calling.style')" prop="type">
        <el-select
          v-model="queryParams.type"
          :placeholder="$t('calling.style')"
          clearable
          size="small"
          style="width: 170px;"
        >
          <el-option
            v-for="dict in auditType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('elButton.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPerm="['dict_del']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDel"
        >{{ $t('elButton.isRead') }}
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <!-- 审核记录-->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('equipment.number')" align="center" prop="id" width="150" />
      <el-table-column :label="$t('program.reviewer')" align="center" prop="checkBy" width="220" />
      <el-table-column :label="$t('program.status')" align="center" prop="status" width="150" :formatter="statusFormat" />
      <el-table-column :label="$t('calling.style')" align="center" prop="type" width="150" :formatter="typeFormat" />
      <el-table-column :label="$t('equipment.programName')" align="center" prop="name" width="130" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('equipment.programSort')" align="center" prop="groupName" width="200" />
      <el-table-column :label="$t('equipment.remarks')" align="center" prop="remarks" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('equipment.isRead')" align="center" prop="isRead" width="150" :formatter="isReadFormat" />
      <el-table-column :label="$t('wel.info.time')" align="center" prop="checkTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { auditRecordAll, setIsRead } from '@/api/ips/audit'

export default {
  name: 'LoginLog',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 类型数据字典
      typeOptions: [],
      // 状态数据字典
      statusOptions: [],
      // 审核类型
      auditType: [],
      // 已读类型
      isReadOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        id: undefined,
        checkBy: '',
        status: undefined,
        type: ''
      },
      // 显示搜索条件
      showSearch: true
    }
  },
  created() {
    this.getList()
    this.getDicts('audit_type').then(response => {
      this.auditType = response.data
    })
    this.getDicts('ata_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('is_read').then(response => {
      this.isReadOptions = response.data
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      auditRecordAll(this.queryParams).then(response => {
        this.list = response.data
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm(this.$t('program.setIsRead') + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return setIsRead(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('Success'))
        this.$router.go(0)
      }).catch(function() {
      })
    },
    // 登录状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.auditType, row.type)
    },
    isReadFormat(row, column) {
      return this.selectDictLabel(this.isReadOptions, row.isRead)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
